using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using IGlobalist.Cms.Common;
using IGlobalist.Chat.Engine;

namespace IGlobalist.Cms.Chat
{
    public class ChatBasePage: BasePage
    {
        #region -------------------- Chat---------------------
        public static string GetUserID(string username, string roomID)
        {
            string userID = string.Format("{0}_{1}", username, roomID);
            return userID;
        }



        protected static User CurrentUser
        {
            get
            {
                try
                {
                    return (User)HttpContext.Current.Session[SessionVariable.Chat.CurrentUser];
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            set
            {
                HttpContext.Current.Session[SessionVariable.Chat.CurrentUser] = value;
            }
        }

        public static string ArchiveVirtualPath
        {
            get
            {
                return ConfigurationSettings.AppSettings.Get("ArchiveFolder");
            }
        }

        public static string ArchivePhysicalPath
        {
            get
            {
                string path = HttpContext.Current.Server.MapPath(ArchiveVirtualPath);
                if (!System.IO.Directory.Exists(path))
                    System.IO.Directory.CreateDirectory(path);
                return path;
            }
        }


        public static string CurrentUserID
        {
            get
            {
                try
                {
                    return CurrentUser.ID;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }

        public Room GetChatRoom(string roomID)
        {
            Room room = RoomConfig.GetChatRoom(roomID);
            room = IGlobalist.Chat.Engine.ChatEngine.AddOrGetRoom(room);
            if (room.GetUser(room.Administrator.ID) == null)
            {
                room.Administrator.Status = IGlobalist.Chat.Engine.Common.UserStatus.IDE;
                room.JoinRoom(room.Administrator);
            }
            return room;
        }

        #endregion
    }
}
